package com.android.support.lib.universal.event;

import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.TranslateAnimation;

import com.android.support.lib.universal.base.BaseApplication;
import com.android.support.lib.universal.util.Utilscreen;
import com.android.support.lib.universal.view.general.LayoutImageButton;

/**
 * 自定义拖动事件
 * 使用方式：
 * view.setOnTouchListener(new MotionEvent(view,true));
 * Created by 彭治铭 on 2016/5/10.
 */
public class DragMotionEvent implements OnTouchListener {
    private LayoutImageButton mTouchView;
    private Utilscreen utilscreen;
    private int state_pressed = 0;
    private int state_general = 0;
    private int screenWidth, screenHeight;
    private int lastX, lastY;
    //记录控件当前layout位置
    private int left, top, right, bottom;
    //控件移动前坐标和移动后坐标
    float fromXDelta, toXDelta, fromYDelta, toYDelta;
    //是否需要吸附边缘效果
    private Boolean isAdsorption = false;

    /**
     * @param mTouchView    拖动控件
     * @param isAdsorption  是否添加吸附效果
     * @param state_general 手指离开时背景样式(Drawable)
     * @param state_pressed 手指按下时背景样式(Drawable)
     */
    public DragMotionEvent(LayoutImageButton mTouchView, Boolean isAdsorption, int state_general, int state_pressed) {
        this.mTouchView = mTouchView;
        this.isAdsorption = isAdsorption;
        this.state_general = state_general;
        this.state_pressed = state_pressed;
        utilscreen = new Utilscreen(BaseApplication.getInstance());
        screenWidth = utilscreen.getScreenWidth();
        screenHeight = utilscreen.getScreenHeight();
    }

    //计算滑动速度，区分触摸和点击事件。
    private VelocityTracker mVelocityTracker = null;

    //初始化VelocityTracker
    private void acquireVelocityTracker(final MotionEvent event) {
        if (null == mVelocityTracker) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
    }

    //释放VelocityTracker
    private void releaseVelocityTracker() {
        if (null != mVelocityTracker) {
            mVelocityTracker.clear();
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
    }

    private long downTime = 0;
    private long upTime = 0;
    private float velocityX = 0;
    private float velocityY = 0;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        acquireVelocityTracker(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                fromXDelta = mTouchView.getX();
                fromYDelta = mTouchView.getY();
                downTime = System.currentTimeMillis();
                //实时更新屏幕的宽度和高度
                screenWidth = utilscreen.getScreenWidth();
                screenHeight = utilscreen.getScreenHeight();
                mTouchView.setBackgroundResource(state_pressed);
                velocityX = 0;
                velocityY = 0;
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                // 必不可少,意思为指定时间内运动了多少个像素，1000是一秒
                mVelocityTracker.computeCurrentVelocity(1000);
                velocityX = mVelocityTracker.getXVelocity();// X速度
                velocityY = mVelocityTracker.getYVelocity();//
                //Log.e("test","velocityX:"+velocityX+" velocityY:"+velocityY);
                if (!(Math.abs(velocityX) < 60 && Math.abs(velocityY) < 60) && !(velocityX == 0 && velocityY == 0)) {
                    mTouchView.setUser(true);
                    move(event);
                }
                break;
            case MotionEvent.ACTION_UP:
                toXDelta = mTouchView.getX();
                toYDelta = mTouchView.getY();
                upTime = System.currentTimeMillis();
                mTouchView.setBackgroundResource(state_general);
                if (isAdsorption) {
                    adsorption();
                }
                //记录控件位置
                mTouchView.setLayout();
                mTouchView.setUser(false);
                if ((toXDelta == fromXDelta) && (toYDelta == fromYDelta) && (upTime - downTime) < 2000) {
                    //点击事件
                    return false;
                }
                return true;
            default:
                break;
        }
        releaseVelocityTracker();
        return false;
    }

    //拖动时，可越过屏幕的尺度。
    private int move_coross = 0;

    //拖动效果
    public void move(MotionEvent event) {
        int dx = (int) event.getRawX() - lastX;
        int dy = (int) event.getRawY() - lastY;

        left = mTouchView.getLeft() + dx;
        top = mTouchView.getTop() + dy;
        right = mTouchView.getRight() + dx;
        bottom = mTouchView.getBottom() + dy;
        if (left < 0 - move_coross) {
            left = 0 - move_coross;
            right = left + mTouchView.getWidth();
        }
        if (right > screenWidth + move_coross) {
            right = screenWidth + move_coross;
            left = right - mTouchView.getWidth();
        }
        if (top < 0 - move_coross) {
            top = 0 - move_coross;
            bottom = top + mTouchView.getHeight();
        }
        if (bottom > screenHeight + move_coross) {
            bottom = screenHeight + move_coross;
            top = bottom - mTouchView.getHeight();
        }
        mTouchView.layout(left, top, right, bottom);
        lastX = (int) event.getRawX();
        lastY = (int) event.getRawY();
    }

    //吸附时，可越过屏幕的尺度
    int adsorptione_coross = 0;

    //吸附屏幕边缘效果
    public void adsorption() {
        fromXDelta = mTouchView.getX();
        fromYDelta = mTouchView.getY();
        if (left + mTouchView.getWidth() / 2 <= screenWidth / 2) {
            //左====================================================================================
            if (top + mTouchView.getHeight() / 2 <= screenHeight / 2) {
                //上--------------------------------------------------------------------------------
                if (left / 2.6 <= top) {
                    //左
                    mTouchView.layout(0, top, mTouchView.getWidth(), bottom);
                    if (top < -adsorptione_coross) {
                        mTouchView.layout(0, adsorptione_coross, mTouchView.getWidth(), mTouchView.getHeight() + adsorptione_coross);
                    }
                } else {
                    //上
                    mTouchView.layout(left, 0, right, mTouchView.getHeight());
                    if (left < -adsorptione_coross) {
                        mTouchView.layout(adsorptione_coross, 0, mTouchView.getWidth() + adsorptione_coross, mTouchView.getHeight());
                    }
                }
            } else {
                //下--------------------------------------------------------------------------------
                if (left / 2.6 <= screenHeight - bottom) {
                    //左
                    mTouchView.layout(0, top, mTouchView.getWidth(), bottom);
                    if (bottom > adsorptione_coross + screenHeight) {
                        mTouchView.layout(0, adsorptione_coross + screenHeight - mTouchView.getHeight(), mTouchView.getWidth(), adsorptione_coross + screenHeight);
                    }
                } else {
                    //下
                    mTouchView.layout(left, screenHeight - mTouchView.getHeight(), right, screenHeight);
                    if (left < -adsorptione_coross) {
                        mTouchView.layout(adsorptione_coross, screenHeight - mTouchView.getHeight(), mTouchView.getWidth() + adsorptione_coross, screenHeight);
                    }
                }
            }
        } else {
            //右====================================================================================
            if (top + mTouchView.getHeight() / 2 <= screenHeight / 2) {
                //上--------------------------------------------------------------------------------
                if ((screenWidth - right) / 2.6 <= top) {
                    //右
                    mTouchView.layout(screenWidth - mTouchView.getWidth(), top, screenWidth, bottom);
                    if (top < -adsorptione_coross) {
                        mTouchView.layout(screenWidth - mTouchView.getWidth(), adsorptione_coross, screenWidth, mTouchView.getHeight() + adsorptione_coross);
                    }
                } else {
                    //上
                    mTouchView.layout(left, 0, right, mTouchView.getHeight());
                    if (right > adsorptione_coross + screenWidth) {
                        mTouchView.layout(adsorptione_coross + screenWidth - mTouchView.getWidth(), 0, adsorptione_coross + screenWidth, mTouchView.getHeight());
                    }
                }
            } else {
                //下--------------------------------------------------------------------------------
                if ((screenWidth - right) / 2.6 <= (screenHeight - bottom)) {
                    //右
                    mTouchView.layout(screenWidth - mTouchView.getWidth(), top, screenWidth, bottom);
                    if (bottom > adsorptione_coross + screenHeight) {
                        mTouchView.layout(screenWidth - mTouchView.getWidth(), adsorptione_coross + screenHeight - mTouchView.getHeight(), screenWidth, adsorptione_coross + screenHeight);
                    }
                } else {
                    //下
                    mTouchView.layout(left, screenHeight - mTouchView.getHeight(), right, screenHeight);
                    if (right > adsorptione_coross + screenWidth) {
                        mTouchView.layout(adsorptione_coross + screenWidth - mTouchView.getWidth(), screenHeight - mTouchView.getHeight(), adsorptione_coross + screenWidth, screenHeight);
                    }
                }
            }
        }
        toXDelta = mTouchView.getX();
        toYDelta = mTouchView.getY();
        TranslateAnimation TranslateAnimation = new TranslateAnimation(fromXDelta - toXDelta, 0, fromYDelta - toYDelta, 0);
        TranslateAnimation.setDuration(260);
        TranslateAnimation.setFillAfter(true);
        mTouchView.startAnimation(TranslateAnimation);
    }
}
